package projectsys.core.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("rawtypes")
public class ToolUtils {
	
	/******
	 * 用于获取后台传来的map数值
	 * @author 
	 *
	 */
	public static String getValue(Map map,String pro){
		String str = "";
		if(map.get(pro.toUpperCase())!=null&&!map.get(pro.toUpperCase()).equals("")){
			str = map.get(pro.toUpperCase()).toString();
		}
		return str;
	}
	
	/**
	 * 将判断后的值返回页面
	 * @param sign 查询出来的标记值
	 * @param response
	 */
	public static void queryValidate(String sign,HttpServletResponse response){
		try {
			PrintWriter out = response.getWriter();
			response.setHeader("Cache-Control", "no-cache");
			response.setContentType("text/json;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			out.print(sign);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 用于获取页面请求的值如果是null转换为空字符串
	 * @param request
	 * @param name属性名的名字
	 * @return
	 */
	public static String getParameter(HttpServletRequest request,String name){
		String str="";
		if(request.getParameter(name)!=null){
			str = request.getParameter(name);
		}
		return str;
	}
	
	/*******
	 * 手机短信提醒功能调用（待研究）
	 * @param dhhm 发送短信的移动电话号码
	 * @param nr 发送的内容信息。
	 */
	public static void sjDxTx(String dhhm,String nr){
		try {
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			String user = "sa"; 
			String password = "hagaj"; 
			String url = "jdbc:microsoft:sqlserver://10.37.128.206:1433;DatabaseName=PoliceSmsForce"; 
			Connection conn = DriverManager.getConnection(url,user,password);
			String sql="insert into psf_tbl_msg_reply(requestid,sendernumber,replytonumber,replycontent,replydatetime)" +
					" values('0','"+dhhm+"','"+dhhm+"','"+nr+"','"+DateUtils.getCurrentTimeAsString()+"')";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.execute();
			pstmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
	}
}
